package com.sunrise.gateway.web.core;

import com.dap.exception.PlatformException;
import com.dap.param.GeneratePlatformCtrlDataInterface;
import com.dap.param.StringInput;
import com.sunrise.gateway.commons.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/****
 * 参数通过post请求的参数过来, 没有权限信息
 * @author wangmin
 */
@Service
public class StringInputBodyPostRequestEngine extends EngineBase {
    private final static Logger LOGGER = LoggerFactory.getLogger(StringInputBodyPostRequestEngine.class);

    @Autowired
    private GeneratePlatformCtrlDataInterface generatePlatformCtrlDataInterface;

    public String execute(HttpServletRequest req, String leaveOutGroupNamePrefix, String internalRequestSource) {
        String group = getGroupName(req, leaveOutGroupNamePrefix);

        String inputBody;
        try {
            inputBody = CommonUtils.inputStream2String(req.getInputStream());
            if (null == inputBody || inputBody.length() <= 4) {
                return null;
            }
        } catch (Exception e) {
            LOGGER.warn("", e);
            throw new PlatformException("获取数据流失败");
        }

        StringInput stringInput = new StringInput(generatePlatformCtrlDataInterface.generate(internalRequestSource), inputBody);
        return executeRequestStr(group, stringInput, true);
    }
}
